package com.xlcszsgc.module.clob.bao.impl;

import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import com.xlcszsgc.core.basedao.base.impl.BaseDao;
import com.xlcszsgc.core.util.PropertyUtil;
import com.xlcszsgc.module.clob.bao.IClobDao;
import com.xlcszsgc.module.clob.domain.BaseClob;
import com.xlcszsgc.module.clob.ui.commond.BaseClobCommond;

/**
 * @Title: ClobDaoImpl.java
 * @Package com.xlcszsgc.module.clob.bao.impl
 * @Description: TODO
 * @author Zhaoqt  
 * @date 2014-5-23 上午1:46:25
 * @version V1.0
 */
@Repository(value = "com.xlcszsgc.module.clob.bao.impl.ClobDaoImpl")
public class ClobDaoImpl extends BaseDao implements IClobDao {

	@Override
	public void addClob(BaseClob baseClob) throws Exception {
		this.addEntity(baseClob);
	}

	@Override
	public void deleteClobById(BaseClob baseClob) throws Exception {
		this.deleteEntityByPK(baseClob);
	}

	@Override
	public void updateClob(BaseClob baseClob) throws Exception {
		this.updateEntityByPK(baseClob);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BaseClob> listClob(BaseClobCommond baseClobCommond,
			int firstResult, int maxResult) throws Exception {
		if(PropertyUtil.objectNotEmpty(baseClobCommond.getQueryString())) {
			Session session = this.getHibernateSession();
			Map<String, Object> paramMap = baseClobCommond.getParamMap();
			Query query = session.createQuery(
					baseClobCommond.getQueryString());
			if ((paramMap != null) && (paramMap.size() > 0)) {
		          query.setProperties(paramMap);
		    }
			if (firstResult > 0) {
				query.setFirstResult(firstResult);
	        }
	        if (maxResult > 0) {
	        	query.setMaxResults(maxResult);
	        }
	        query.setResultTransformer(Transformers.aliasToBean(BaseClob.class));  
	        baseClobCommond.setResultCollection(query.list());
			return (List<BaseClob>)baseClobCommond.getResultCollection();
		}
		return null;
	}

	@Override
	public Long countBlob(BaseClobCommond baseClobCommond) throws Exception {
		if(PropertyUtil.objectNotEmpty(baseClobCommond.getQueryString())) {
			Session session = this.getHibernateSession();
			Map<String, Object> paramMap = baseClobCommond.getParamMap();
				int startIndex = -1;
				String countHQL = baseClobCommond.getQueryString(); 
		    	countHQL = countHQL.substring(countHQL.toUpperCase().indexOf("FROM"), countHQL.length());
		        
		    	if ((startIndex = countHQL.toUpperCase().indexOf("ORDER BY")) != -1) {
		        	countHQL = countHQL.substring(0, startIndex);
		        } 
		        
		        Query query = null;
		        query = session.createQuery("SELECT COUNT(*) " + countHQL);
				if ((paramMap != null) && (paramMap.size() > 0)) {
			          query.setProperties(paramMap);
			    }
				return (Long)query.list().iterator().next();
		} 
		return Long.valueOf(0);
	}

	@Override
	public BaseClob findObject(BaseClobCommond baseClobCommond)
			throws Exception {
		return (BaseClob)this.findObject(BaseClob.class, baseClobCommond.getSearchClobId());
	}

	@Override
	public void dynamicUpdateClob(BaseClobCommond baseClobCommond)
			throws Exception {
		if(PropertyUtil.objectNotEmpty(baseClobCommond.getQueryString())) {
			Session session = this.getHibernateSession();
			Map<String, Object> paramMap = baseClobCommond.getParamMap();
			Query query = session.createQuery(baseClobCommond.getQueryString());
			if ((paramMap != null) && (paramMap.size() > 0)) {
		          query.setProperties(paramMap);
		    }
			query.executeUpdate();
		}
	}
	
}